package cn.edu.neosoft.sunbo509.finefood.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Objects;

import cn.edu.neosoft.sunbo509.finefood.BaseUrl;
import cn.edu.neosoft.sunbo509.finefood.Bean.RegisterBean;
import cn.edu.neosoft.sunbo509.finefood.R;
import cn.edu.neosoft.sunbo509.finefood.Service.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RegisterActivity extends AppCompatActivity  implements View.OnClickListener{
    private EditText et_username,et_password,et_identify,et_address,et_phone,et_comment;
    private Button btn_reg,btn_return;
    private  String username,pwd,newpwd,phone,address,comment;
    private Service service;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        InitView();
    }


    public  void  InitView(){
        et_username=(EditText)findViewById(R.id.et_username);
        et_password=(EditText)findViewById(R.id.et_pwd);
        et_identify=(EditText)findViewById(R.id.et_idetypwd);
        et_address=(EditText)findViewById(R.id.et_address);
        et_phone=(EditText)findViewById(R.id.et_phone);
        et_comment=(EditText)findViewById(R.id.et_comment);
        btn_reg=(Button)findViewById(R.id.btn_commit);
        btn_return=(Button)findViewById(R.id.btn_return);
        btn_reg.setOnClickListener(this);
        btn_return.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.btn_commit:
                username=et_username.getText().toString();
                pwd=et_password.getText().toString();
                newpwd=et_identify.getText().toString();
                address=et_address.getText().toString();
                phone=et_phone.getText().toString();
                comment=et_comment.getText().toString();
                if(TextUtils.isEmpty(username)
                        ||TextUtils.isEmpty(pwd)
                        || TextUtils.isEmpty(newpwd)
                        ||TextUtils.isEmpty(address)
                        ||TextUtils.isEmpty(phone)
                        ||TextUtils.isEmpty(comment))
                    Toast.makeText(getApplication(),"你的信息有空，请检查后提交",Toast.LENGTH_SHORT).show();
                else if(!pwd.equals(newpwd))
                    Toast.makeText(getApplication(),"你的密码不一致，请检查后提交",Toast.LENGTH_SHORT).show();
                else {
                    GetDate();
                }
                break;
            case R.id.btn_return:
                Intent intent1=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent1);
                break;
           default:
               break;
        }

    }
    public void GetDate(){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service=retrofit.create(Service.class);
        Call<RegisterBean>call=service.getRegister(username,pwd,phone,address,comment);
        call.enqueue(new Callback<RegisterBean>() {
            @Override
            public void onResponse(Call<RegisterBean> call, Response<RegisterBean> response) {
                String success = response.body().getSuccess();
                if (success.equals("1")) {
                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    intent.putExtra("message", username);
                    startActivity(intent);
                    Toast.makeText(getApplication(), "注册成功", Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(getApplication(), "用户名已被使用，注册失败", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<RegisterBean> call, Throwable t) {
                Log.e("error", t.getMessage());
            }
        });


    }

}
